package com.xxh.chatdemo.task;

import cn.hutool.core.lang.Dict;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.xxh.chatdemo.config.Event;
import com.xxh.chatdemo.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Slf4j
@Component
public class wsTask {

    @Autowired
    private SocketIOServer server;

    @Autowired
    RedisUtil redisUtil;

    @OnEvent(Event.WATCH)
    @Scheduled(cron = "*/2 * * * * ?")
    public void pushOnlineInfo(){
        //获取所有连接上的用户他们的信息
        Set<String> keys = redisUtil.keys("user*");
        Collection<SocketIOClient> clients = server.getAllClients();
        List<Object> list = new ArrayList<>();
        for (String k:keys){
            Map map=new HashMap<String,String>();
            map.put("name",k);
            map.put("img","/image/three.jpeg");
            list.add(map);
        }
        for (SocketIOClient c:clients){
            c.sendEvent(Event.WATCH,list);
        }
    }
}
